--[[
Name: BrownieHelper
Developed by: Sonora (The Dragon Flight)
Website: http://sonora.kirintor.googlepages.com/contents
SVN: http://code.google.com/p/browniehelper/source/browse
License: GNU General Public License v3
]]

function ViewItemsFrame:Initialize(app)
    
    --Save the app
    self.app = app
    
    --Register for gui events
    self:SetScript("OnShow", function() self:OnShow() end)
    self:SetScript("OnHide", function() self:OnHide() end)
    
    --Do some basic setup of our table
    ViewItemsTable:setWidths({205,150})
    ViewItemsTable:setFields({"Item","Auction order"})
    ViewItemsTable:registerSelectionCallback(function(row, selected) self:handleRowSelected(row, selected) end)
    ViewItemsTable:registerRowEnterCallback(function(row) self:rowEnter(row) end)
    ViewItemsTable:registerRowLeaveCallback(function(row) self:rowLeave(row) end)
    ViewItemsTable:registerDoubleClickCallback(function(row) if ViewItemsTable:getSelections()[1] then self:OnStartAuction() end end)
    
    --Update when the loot is changed
    self.app:registerEvent("BrownieHelper_LootChanged", function() self:updateView() end)
    self.app:registerEvent("BrownieHelper_ModeChanged", function(mode) self:OnModeChanged(mode) end)
    
    --Hook in a loot link watcher, but only watch if we're shown
    hooksecurefunc("SetItemRef", function(link, text, button) self:handleLootLink(link, text, button) end)
    self.watchLinks = false
    
end

function ViewItemsFrame:handleLootLink(link, text, button)
    if self.watchLinks and self.app:getMode()>=BROWNIEHELPER_MASTERLOOTER then
        
        --Let the api do the link parsing
        local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount, itemEquipLoc, itemTexture = GetItemInfo(link)
        
        --Create a new lootInfo hash
        local lootInfo = {}
        lootInfo.link = itemLink
        lootInfo.icon = itemTexture
        lootInfo.name = itemName
        lootInfo.quantity = 1
        lootInfo.rarity = itemRarity
        lootInfo.biddingsystems = getBiddingSystems(itemName, self.app)
        
        self.app:addLoot(lootInfo)
    end
end

function ViewItemsFrame:handleRowSelected(row, selected)
    
    if self.app:getMode()<BROWNIEHELPER_MASTERLOOTER then
        ViewItemsStartAuctionButton:Disable()
        ViewItemsEditSystemsButton:Disable()
        ViewItemsRemoveItemButton:Disable()
        ViewItemsCloseButton:SetText("Close")
        ViewItemsCurrentPoolLabel:SetText("These items are going to be comming up for auction.")
        ViewItemsAnnounceButton:Disable()
    else
        ViewItemsCloseButton:SetText("Cancel")
        ViewItemsCurrentPoolLabel:SetText("You can add items to this list by clicking on an item link.")
        ViewItemsAnnounceButton:Enable()
        if selected==true then
            ViewItemsStartAuctionButton:Enable()
            ViewItemsEditSystemsButton:Enable()
            ViewItemsRemoveItemButton:Enable()
        else
            ViewItemsStartAuctionButton:Disable()
            ViewItemsEditSystemsButton:Disable()
            ViewItemsRemoveItemButton:Disable()
        end
    end
    
end

function ViewItemsFrame:OnModeChanged(mode)
    self:handleRowSelected()
end

--Event handlers
function ViewItemsFrame:OnShow()
    --updates buttons
    self:handleRowSelected(nil, false)
    
    --Only watch links if we're shown
    self.watchLinks = true
    
    --clear any table selection
    ViewItemsTable:clearSelections()
    
    --Whenever we're shown, reset the phase indexes on our items
    for index, itemInfo in pairs(self.app:getLoot()) do
        itemInfo.phaseIndex = 0
    end
    
end
function ViewItemsFrame:OnHide()
    --Only watch links if we're shown
    self.watchLinks = false
    self.app:clearLootUpdates()
end

function ViewItemsFrame:updateView()
    
    --Build the table rows from the loot list tracked on app
    ViewItemsTable:clearRows()
    for index, itemInfo in pairs(self.app:getLoot()) do
        itemInfo["Item"] = itemInfo.link
        itemInfo["Auction order"] = valueJoin(fromValues(itemInfo.biddingsystems,'name'), ", ")
        ViewItemsTable:addRow(itemInfo)
    end
    self:handleRowSelected(nil, false)
    
end

function ViewItemsFrame:OnRemoveItem()
    for i,itemInfo in pairs(ViewItemsTable:getSelections()) do
        self.app:removeLoot(itemInfo)
    end
    self.app:TriggerEvent("BrownieHelper_LootChanged")
end

function ViewItemsFrame:OnEditBiddingSystems()
    EditBiddingSystemFrame:editBiddingSystems(ViewItemsTable:getSelections()[1])
end

function ViewItemsFrame:OnAnnounceItem()
    self.app:sendLootUpdates()
end

function ViewItemsFrame:OnStartAuction()
    self.app:startAuction(ViewItemsTable:getSelections()[1])
    self:Hide()
end

function ViewItemsFrame:OnClose()
    self.watchLinks = false
    self:Hide()
end

function ViewItemsFrame:rowEnter(row)

    --position & feed the game tool tip
    GameTooltip_SetDefaultAnchor( GameTooltip, ViewItemsFrame )
    GameTooltip:ClearAllPoints()
    GameTooltip:SetPoint("TOPLEFT", ViewItemsFrame, "TOPRIGHT", 0, -15)
    GameTooltip:SetHyperlink(row.link)    
    GameTooltip:Show()
    
end

function ViewItemsFrame:rowLeave(row)
    GameTooltip:Hide()
end
    

